package com.hanthink.web.controller.app.repair;

import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.hanthink.common.annotation.Anonymous;
import com.hanthink.system.service.impl.WeChatMessageServiceImpl;
import io.netty.util.internal.StringUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hanthink.common.constant.HttpStatus;
import com.hanthink.common.core.domain.AjaxResult;
import com.hanthink.common.core.domain.model.LoginUser;
import com.hanthink.common.core.page.TableDataInfo;
import com.hanthink.common.utils.ServletUtils;
import com.hanthink.framework.web.service.TokenService;
import com.hanthink.system.domain.repair.Equip;
import com.hanthink.system.domain.repair.EquipRepairVo;
import com.hanthink.system.service.repair.EquipRepairService;

/**
 * <p>
 * Title: EquipRepairController
 * </p>
 * <p>
 * Description: 设备维修控制器
 * </p>
 *
 * @author vondser
 * @date 2020年12月21日
 */
@RestController
@RequestMapping("/equip/repair")
public class EquipRepairController {

    protected final Logger logger = LoggerFactory.getLogger(EquipRepairController.class);

    @Autowired
    private TokenService tokenService;

    @Autowired
    private EquipRepairService equipRepairService;

    @Autowired
    private WeChatMessageServiceImpl weChatMessageService;


    /**
     * 隐患排查
     *
     * @param scv
     * @return
     */
    @PostMapping("/saveReportRepairInfo")
    public AjaxResult saveEquipReportRepairInfo(@RequestBody EquipRepairVo scv) {

        // 获取当前登录用户
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 非空check
        if (ObjectUtil.isEmpty(loginUser)) {
            return AjaxResult.error("用户信息异常");
        }
        if (ObjectUtil.isEmpty(scv)) {
            return AjaxResult.error("请填写数据");
        }
        boolean result = equipRepairService.saveEquipReportRepairInfo(scv, loginUser);
        if (!result) {
            return AjaxResult.error("保存隐患信息失败！");
        }
        return AjaxResult.success();
    }

    /**
     * 检查设备是否存在点检任务
     */
    @PostMapping("/queryEquipInfo")
    public AjaxResult queryEquipInfo(@RequestBody Equip equip) {
        if (ObjectUtils.isEmpty(equip) || StringUtils.isBlank(equip.getEquipCode())) {
            return AjaxResult.error("设备条码不能为空！");
        }
        // 扫描条码，查询设备任务信息
        List<Equip> equipList = equipRepairService.queryEquipInfo(equip);

        AjaxResult ajax = AjaxResult.success();
        if (CollectionUtils.isNotEmpty(equipList)) {
            ajax.put("equip", equipList.get(0));
        }
        return ajax;
    }

    /**
     * 检查设备故障信息
     */
    @PostMapping("/queryEquipFault")
    public TableDataInfo queryEquipFault(@RequestBody Map<String, Object> params) {
        TableDataInfo info = new TableDataInfo();
        info.setCode(HttpStatus.SUCCESS);
        info.setMsg("查询成功");
        info.setRows(equipRepairService.queryEquipFault(params));
        return info;
    }
}
